package com.dustedpixels.jasmin.unit.impl;

import com.dustedpixels.jasmin.unit.Unit;
import com.dustedpixels.jasmin.unit.implbase.UnitImplBase;

/**
 * A unit which divides the cycles by specified divider and performs resulting
 * number of cycles on wrapped unit. The effect is like the wrapped unit was
 * performing given number times slower.
 */
public final class CycleDivUnit extends UnitImplBase {
    private final Unit wrappedUnit;

    private final int divider;

    public CycleDivUnit(Unit wrappedUnit, int divider) {
        this.wrappedUnit = wrappedUnit;
        this.divider = divider;
    }

    public int processSlice() {
        return wrappedUnit.processSlice() * divider;
    }

    public int processCycles(int cycles) {
        int wrappedUnitCycles = (cycles + divider - 1) / divider;

        return wrappedUnit.processCycles(wrappedUnitCycles) * divider;
    }
}
